<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class RequestResponseService 
{
	
	public static function process_dadf_request($bid_request) {
		
		$offer_response_id_list = array();
		
		foreach ($bid_request->class_member_only_incoming_request_adbundle_list as $request_adbundle):
		
			foreach ($request_adbundle->class_member_only_incoming_request_advertisement_list as $request_advertisement):
			
				$offer_response_id_list[] = self::createOfferResponse($request_advertisement);
				
			endforeach;
		
		endforeach;
		
		$transformer = new OutgoingOfferTransformer($offer_response_id_list, 'Outgoing');
		header("Content-type: text/xml");
		echo $transformer->xmlData;
	}
	
	public static function createOfferResponse($request_advertisement) {
		
		$offer_amount 	= null;
		$offer_type		= null;
		$cashdeal_type  = null;
		
		$bid_request_workflow 				= new BidRequestWorkflow();
		
		$bid_request_accepted 				= $bid_request_workflow->decide_bid_request_offer($request_advertisement, $offer_amount, $offer_type, $cashdeal_type);
		
		$partner_id 						= Zend_Registry::get('identification.partnerid');
		$partner_name 						= Zend_Registry::get('identification.partnername');
		
		$offer_response = new OutgoingOfferResponse();
		$offer_response->Accept				= $bid_request_accepted == true ? 1 : 0;
		$offer_response->ID					= $request_advertisement->ID;
		$offer_response->OfferType			= $offer_type;
		$offer_response->Price				= $offer_amount;
		
		if ($cashdeal_type !== null):
			$offer_response->CashDeal		= $cashdeal_type;
		endif;
		
		$offer_response->MinutesToExpire 	= 60;
		$offer_response->Message			= $bid_request_accepted == true ? "Success" : "Rejected";
		$offer_response->AcceptID			= rand(121892,84983489);
		$offer_response->PartnerID			= $partner_id;
		$offer_response->PartnerName		= $partner_name;
		$offer_response->save();
		
		return $offer_response->OutgoingOfferResponseID;
		
	}
	

}